define([
        "dojo/_base/declare",
//        "widgets/baseWidget/BaseWidget",
        "gistech/widgets/baseWidget/BaseWidget",
        "dojo/topic",
        "dojo/text!./templates/Popup.html","gistech/widgets/normalDialog/NormalDialog"],
        function(declare,BaseWidget,topic,template,NormalDialog){
            return declare([BaseWidget],{
//			return declare([NormalDialog],{
            	
            	alignNode:null,//参照的dom节点，popup的尖角指向该节点的下沿中心位置
                close:false,//是否需要显示“关闭”，默认不显示
                hideWhenClickBody:false,//当点击body时，是否隐藏popup。默认不隐藏
                _handle:null,//订阅"隐藏popup"的句柄

                postMixInProperties:function () {
                    var baseDialogTemplateStr = template;
                    this.templateString = baseDialogTemplateStr.replace("{content}", this.templateString);
                    this.inherited(arguments);
                },

                constructor:function (args) {
                    if (args.alignNode) {
                        this.alignNode = args.alignNode;
                    }
                    if (args.close) {
                        this.close = args.close;
                    }
                    if (args.hideWhenClickBody) {
                        this.hideWhenClickBody = args.hideWhenClickBody;
                    }
                },

                postCreate:function () {
                    if (this.close) {
                        dojo.style(this.btnClose, "display", "inline-block");
                        dojo.connect(this.btnClose, "onclick", this, this.closeDialog);
                    }
                    dojo.addClass(this.alignNode, "selected");//选中
                    dojo.connect(window, "onresize", this, this.onWindowResize);
                    dojo.place(this.domNode, dojo.body());
                    this._position();
                    this._handle = topic.subscribe("hidePanel", dojo.hitch(this, this._hidePopup));
                },

                //窗口尺寸改变的listener
                onWindowResize:function () {
                    this._position();
                },

                //定位，外层和内层的尖角分别定位
                _position:function () {
                    var alignNodeSize = dojo.coords(this.alignNode);//参照节点的尺寸
                    var triangleSize = dojo.coords(this.triangle);//尖角的尺寸
                    var popupSize = dojo.coords(this.domNode);//popup的尺寸
                    var windowSize = dojo.window.getBox();//窗口的尺寸

                    var popupSpanL = alignNodeSize.x + alignNodeSize.w / 2 - popupSize.w / 2;//popup与窗口的左边距
                    var popupSpanR = windowSize.w - (alignNodeSize.x + alignNodeSize.w / 2 + popupSize.w / 2);//popup与窗口的右边距

                    var popupL, popupT, trgL;
                    if (popupSpanL < 0) {
                        popupL = 0;
                        trgL = alignNodeSize.x + alignNodeSize.w / 2 - triangleSize.w / 2;
                    }
                    else if (popupSpanR < 0) {
                        popupL = windowSize.w - popupSize.w;
                        trgL = alignNodeSize.x + alignNodeSize.w / 2 - triangleSize.w / 2 - popupL;
                    }
                    else {
                        popupL = popupSpanL;
                        trgL = alignNodeSize.x + alignNodeSize.w / 2 - triangleSize.w / 2 - popupL;
                    }
                    popupT = alignNodeSize.y + alignNodeSize.h + triangleSize.h;

                    dojo.style(this.domNode, "top", popupT + "px");
                    dojo.style(this.domNode, "left", popupL + "px");
                    dojo.style(this.triangle, "left", trgL + "px");
                },

                //初始化时由basewidget从config的label取值直接替换模板中的{title}
                setTitle:function (title) {
                    this.headTitle.innerHTML = title;
                },

                //隐藏窗口
                _hidePopup:function (dom) {
                    if (this.hideWhenClickBody) {
                        if (dojo.style(this.domNode, "display") == "none") {
                            //点击的是alignNode，显示popup
                            if (dom == this.alignNode) {
                                this.onShow();
                            }
                        }
                        else {
                            //点击的是popup之外，隐藏popup
                            if (!this.domNode.contains(dom)) {
                                this.onHide();
                            }
                        }
                    }
                },

                closeDialog:function () {
                    //退订隐藏popup的主题
                    if (this._handle) {
                        this._handle.remove();
                    }
        			topic.publish("BookMarkDestory");//摧毁绘制组件
                    dojo.removeClass(this.alignNode, "selected");
                    var def = this.onBeforeClose();
                    if (def && def.then) {
                        def.then(dojo.hitch(this, this.onClose));
                    }
                    else {
                        this.onClose();
                    }
                },


                onBeforeClose:function () {},

                onAfterClose:function () {},

                onClose:function () {
                    this.destroy();
                    this.onAfterClose();
                },

                onShow:function () {
                    dojo.addClass(this.alignNode, "selected");
                    esri.show(this.domNode);
                },

                onHide:function () {
                    dojo.removeClass(this.alignNode, "selected");
                    esri.hide(this.domNode);
                },

                startup:function () {
                    this._position();
                    this.inherited(arguments);
                }
            })
});